package vn.edu.saigontech.ncms.web.system;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import vn.edu.saigontech.ncms.model.user.LoggingRecord;
import vn.edu.saigontech.ncms.model.user.User;
import vn.edu.saigontech.ncms.service.LoggingRecordService;
import vn.edu.saigontech.ncms.service.UserService;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.web.BaseAction;

@SuppressWarnings("serial")
public class LoggingAction extends BaseAction {
	private Long id;
	private Long userId;
	private String fromDate;
	private String toDate;
	private String action;
	private String description;
	Collection<LoggingRecord> records;

	private List<String> getActionTypes() {
		List<String> actions = new ArrayList<String>();
		actions.add(BaseAction.ADD);
		actions.add(BaseAction.EDIT);
		actions.add(BaseAction.DELETE);
		actions.add(BaseAction.SEARCH);
		actions.add(BaseAction.REPORT);
		return actions;
	}
	
	public String showSearch() {
		Collection<User> users = UserService.INSTANCE.findAllNotAdmin();
		REQUEST().setAttribute("users", users);
		REQUEST().setAttribute("actions", getActionTypes());
		return SUCCESS;
	}
	
	public String search() {
		User user = UserService.INSTANCE.findById(this.userId);
		Date fromDateDate = DateUtil.fromString(this.fromDate, this.i18n("format.date"));
		Date toDateDate = DateUtil.fromString(this.toDate, this.i18n("format.date"));
		this.records = LoggingRecordService.INSTANCE.find(user, fromDateDate, toDateDate, action, description);
		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getUserId() {
		return userId;
	}

	public void setUserId(Long userId) {
		this.userId = userId;
	}

	public String getFromDate() {
		return fromDate;
	}

	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}

	public String getToDate() {
		return toDate;
	}

	public void setToDate(String toDate) {
		this.toDate = toDate;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Collection<LoggingRecord> getRecords() {
		return records;
	}

	public void setRecords(Collection<LoggingRecord> records) {
		this.records = records;
	}
}
